package test.adddata;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import entity.Pubmed;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.PubmedUtils2;
import utils.SmallTool;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022/5/25 9:33
 */
public class 比对pmid获取我缺少的重新下载2022年525 {
    public static void main(String[] args) {
        String s = FileUtil.readUtf8String("D:\\我没有的pmid.log");
        List<Integer> integers = JSONArray.parseArray(s, Integer.class);
        List<List<Integer>> split = ListUtil.split(integers, 100);
        SmallTool.printMess("页数++" + split.size());
        String path = "/usr/local/xml/20220525/";
//        int index = 11;
//        int ss = 1;
//        for (List<Integer> integerList : split) {
//            SmallTool.printMess("页数++" + ss);
//            index++;
//            if (!FileUtil.exist(path + index + ".xml")) {
//                List<String> collect = integerList.stream().map(o -> o.toString()).collect(Collectors.toList());
//                String pmidStr = String.join(",", collect);
//
//                HttpClientConfig config = new HttpClientConfig()
//                        .setUrl("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi")
//                        .build();
//                config.getNvpsList().add(new BasicNameValuePair("db", "pubmed"));
//                config.getNvpsList().add(new BasicNameValuePair("retmode", "xml"));
//                config.getNvpsList().add(new BasicNameValuePair("id", pmidStr));
//                String body = HttpClientUtil.post(config);
//                FileUtil.writeString(body, path + index + ".xml", "UTF-8");
//                SmallTool.SleepMillis(1000L);
//            }
//            ss++;

        File[] ls = FileUtil.ls(path);
        for (File l : ls) {

            String s1 = FileUtil.readString(l, "UTF-8");
            Document doc = Jsoup.parse(s1);
            try {
                // 主要是用于进行过滤掉参文房子参文doi混入文章doi
                doc.select("ReferenceList").remove();
            } catch (Exception e) {

            }
            String html = doc.html();
            List<Element> divList = new ArrayList<>();
            Elements pubmedArticles = doc.select("PubmedArticle");
            Elements pubmedBookArticles = doc.select("PubmedBookArticle");
            for (Element e : pubmedArticles) {
                divList.add(e);
            }
            for (Element e : pubmedBookArticles) {
                divList.add(e);
            }
            //
            List<Pubmed> pubmeds = PubmedUtils2.parseHtml(divList);
            System.out.println(pubmeds);

        }


//        }
    }
}
